package com.chinafree.permission.model.bo;

import com.chinafree.permission.model.po.RolePermissionPO;
import lombok.*;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RolePermissionEditBO {

    private Integer roleId;
    private List<Integer> permissionIdList;

    public Optional<List<RolePermissionPO>> toRolePermissionPOList() {
        if (CollectionUtils.isEmpty(permissionIdList)) {
            return Optional.empty();
        }
        List<RolePermissionPO> rolePermissionPOList = new ArrayList<>();
        for (Integer permissionId : permissionIdList) {
            RolePermissionPO build = RolePermissionPO.builder().roleId(this.roleId).permissionId(permissionId).build();
            rolePermissionPOList.add(build);
        }
        if (CollectionUtils.isEmpty(rolePermissionPOList)) {
            return Optional.empty();
        }
        return Optional.of(rolePermissionPOList);
    }
}
